/*
Promise.race是一个JavaScript Promise API方法，它接受一个Promises对象数组，返回一个新Promise对象。
返回的Promise对象将在第一个Promise对象（根据传递参数数组的顺序）的状态转换为已解决或已拒绝时进行解决或拒绝。
 */
function myRace(promises) {
    return new Promise((resolve, reject) => {
        promises.forEach(promise => {
            // 这里直接可以将resolve作为then的参数
            promise.then(resolve).catch(reject)
        })
    })
}
const promise1 = new Promise((resolve, reject) => {
    setTimeout(() => resolve('Promise 1 resolved'), 4000);
});

const promise2 = new Promise((resolve, reject) => {
    setTimeout(() => resolve('Promise 2 resolved'), 3000);
});

const promise3 = new Promise((resolve, reject) => {
    setTimeout(() => resolve('Promise 3 resolved'), 2000);
});
const promise4 = new Promise((resolve, reject) => {
    setTimeout(() => reject('Promise 4 rejected'), 1000);
});
// 就看谁最先调用then的回调函数了 也就是看谁先执行resolve/reject
myRace([promise4, promise2, promise1, promise3])
    .then(result => console.log('Race resolved with:', result), error => console.log('Race rejected with:', error))
